package com.kobeliu.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * leetcode 17
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * 示例 1：
 * 输入：digits = "23"
 * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
 *
 * 示例 2：
 * 输入：digits = ""
 * 输出：[]
 *
 * 示例 3：
 * 输入：digits = "2"
 * 输出：["a","b","c"]
 */

public class Demo_19_No17 {

    public static void main(String[] args) {

        List<String> strings = letterCombinations("23");
        System.out.println(strings.toString());


    }

    public static List<String> letterCombinations(String digits) {
        char ch = 0;
        Map<Integer,List<String>> dpMap = new HashMap<Integer, List<String>>();
        List<String> ss = new ArrayList<String>(); //装int转String类型的list

        for (int i = 0; i < digits.length() ; i++) {
            ch = digits.charAt(i);
            ss.add(intToString(ch - '0'));
        }
        int tmp = 0;
        String s = "";
        if(ss.size()!=0){

            while(tmp < ss.size()) {
                s = ss.get(tmp);
                if (tmp == 0) {
                    List<String> strs = new ArrayList<String>(); //装重新组装的string list
                    for (int i = 0; i < s.length(); i++) {
                        strs.add(String.valueOf(s.charAt(i)));
                    }
                    dpMap.put(tmp, strs);
                } else {
                    List<String> strings = dpMap.get(tmp - 1);
                    List<String> strs = new ArrayList<String>(); //装重新组装的string list
                    for (String s1 : strings) {
                        for (int i = 0; i < s.length(); i++) {
                            strs.add(s1 + String.valueOf(s.charAt(i)));
                        }
                    }
                    dpMap.put(tmp, strs);
                }
//                strs.clear();
                tmp++;
            }
        }else{
            return ss;
        }
        return dpMap.get(tmp-1);
    }


    public static String intToString(int num){
        switch (num){
            case 2: return "abc";
            case 3: return "def";
            case 4: return "ghi";
            case 5: return "jkl";
            case 6: return "mno";
            case 7: return "pqrs";
            case 8: return "tuv";
            case 9: return "wxyz";
            default: return "";
        }
    }

}
